package love.bot.forli.group.review.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import love.bot.forli.group.review.bean.Dto.GroupListDto;
import love.bot.forli.group.review.bean.Result;
import love.bot.forli.group.review.bean.Vo.AddGroupVo;
import love.bot.forli.group.review.entity.Question;
import love.bot.forli.group.review.entity.QuestionGroup;
import love.bot.forli.group.review.entity.QuestionOption;
import love.bot.forli.group.review.entity.QuestionType;
import love.bot.forli.group.review.mapper.QuestionGroupMapper;
import love.bot.forli.group.review.mapper.QuestionMapper;
import love.bot.forli.group.review.mapper.QuestionOptionMapper;
import love.bot.forli.group.review.mapper.QuestionTypeMapper;
import love.bot.forli.group.review.service.QuestionGroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Kontori
 */
@Service
public class QuestionGroupServiceImpl extends ServiceImpl<QuestionGroupMapper, QuestionGroup> implements QuestionGroupService {

    private QuestionGroupMapper questionGroupMapper;

    @Autowired
    public void setQuestionGroupMapper(QuestionGroupMapper questionGroupMapper) {
        this.questionGroupMapper = questionGroupMapper;
    }

    private QuestionTypeMapper questionTypeMapper;

    @Autowired
    public void setQuestionTypeMapper(QuestionTypeMapper questionTypeMapper) {
        this.questionTypeMapper = questionTypeMapper;
    }

    private QuestionMapper questionMapper;

    @Autowired
    public void setQuestionMapper(QuestionMapper questionMapper) {
        this.questionMapper = questionMapper;
    }

    private QuestionOptionMapper questionOptionMapper;

    @Autowired
    public void setQuestionOptionMapper(QuestionOptionMapper questionOptionMapper) {
        this.questionOptionMapper = questionOptionMapper;
    }

    @Override
    public Result<List<GroupListDto>> groupList() {
        List<GroupListDto> groupListDtoList = new ArrayList<>();
        questionGroupMapper.selectList(new QueryWrapper<QuestionGroup>()
                .orderByAsc("question_type_id")).forEach(questionGroup -> {
            GroupListDto groupListDto = new GroupListDto();
            BeanUtils.copyProperties(questionGroup, groupListDto);
            String questionTypeName = questionTypeMapper.selectOne(new QueryWrapper<QuestionType>()
                    .eq("question_type_id", groupListDto.getQuestionTypeId())).getTypeName();
            groupListDto.setQuestionTypeName(questionTypeName);
            groupListDto.setQuestionTotal(questionMapper.selectCount(new QueryWrapper<Question>()
                    .eq("question_group_id",questionGroup.getQuestionGroupId())));
            groupListDtoList.add(groupListDto);
        });
        return Result.success(groupListDtoList);
    }

    @Override
    public Result<Object> deleteGroup(Integer groupId) {
        if (questionGroupMapper.selectById(groupId) == null) {
            return Result.fail("对应题组不存在");
        }
        //删除题组对应所有题目及选项
        questionMapper.selectList(new QueryWrapper<Question>()
                .eq("question_group_id", groupId)).forEach(question -> {
            //删除题目选项
            questionOptionMapper.delete(new QueryWrapper<QuestionOption>()
                    .eq("question_id", question.getQuestionId()));
            //删除题目
            questionMapper.deleteById(question);
        });
        //删除题组
        questionGroupMapper.deleteById(groupId);
        return Result.success("删除成功", null);
    }

    @Override
    public Result<Object> addGroup(AddGroupVo addGroupVo) {
        if (questionTypeMapper.selectById(addGroupVo.getQuestionTypeId()) == null) {
            return Result.fail("对应题组类型不存在");
        }
        if (questionGroupMapper.selectOne(new QueryWrapper<QuestionGroup>()
                .eq("group_name", addGroupVo.getGroupName())) != null) {
            return Result.fail("题组名称重复");
        }
        //插入题组
        QuestionGroup questionGroup = new QuestionGroup();
        BeanUtils.copyProperties(addGroupVo, questionGroup);
        questionGroupMapper.insert(questionGroup);
        return Result.success("新增成功", null);
    }
}
